package com.binyouwei.calculator.ui.calculator

import android.content.Context
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.binyouwei.calculator.R
import com.binyouwei.calculator.widget.OvalVerticalButton
import com.binyouwei.calculator.widget.RoundButton

/**
 * @author 宾有为
 * @date 2025/3/21 16:56
 * @link https://blog.csdn.net/baidu_41616022?type=blog
 * @describe：
 **/

@Preview(widthDp = 540, heightDp = 960)
@Composable
fun CalculatorScreen() {
    Column(modifier = Modifier.fillMaxSize()) {
        CountBlack(modifier = Modifier.weight(1f))
        KeyboardBlack(modifier = Modifier.weight(3.5f))
    }
}

var currentValue by mutableStateOf("0")

@Composable
fun CountBlack(modifier: Modifier = Modifier) {
    Row(
        modifier
            .fillMaxWidth()
            .height(300.dp)
            .background(colorResource(id = R.color.bg_gray))) {
        Text(text = currentValue, color = colorResource(id = R.color.font_black),
            fontSize = 40.sp, modifier = Modifier.align(Alignment.CenterVertically),
            textAlign = TextAlign.Center)
    }
}

@Composable
fun KeyboardBlack(modifier: Modifier = Modifier) {
    Row(
        modifier
            .fillMaxSize()
            .background(colorResource(id = R.color.bg_gray_1))
            .padding(10.dp)) {
        Column(modifier = Modifier
            .fillMaxHeight()
            .weight(1f)
            .padding(10.dp),
            horizontalAlignment = Alignment.CenterHorizontally) {
            RoundButton(text = "MC", onClick = {
                CalculatorNativeLib.memoryClear()
            }, modifier = Modifier.weight(1f))
            RoundButton(text = "C", onClick = {
                currentValue = CalculatorNativeLib.clear()
            }, modifier = Modifier.weight(1f))
            RoundButton(text = "7", onClick = {
                currentValue = CalculatorNativeLib.seven()
            }, modifier = Modifier.weight(1f))
            RoundButton(text = "4", onClick = {
                currentValue = CalculatorNativeLib.four()
            }, modifier = Modifier.weight(1f))
            RoundButton(text = "1", onClick = {
                currentValue = CalculatorNativeLib.one()
            }, modifier = Modifier.weight(1f))
            RoundButton(text = "%", onClick = {
                currentValue = CalculatorNativeLib.percentage()
            }, modifier = Modifier.weight(1f))
        }
        Column(modifier = Modifier
            .fillMaxHeight()
            .weight(1f)
            .padding(10.dp),
            horizontalAlignment = Alignment.CenterHorizontally) {
            RoundButton(text = "M+", onClick = {
                CalculatorNativeLib.memoryPlus()
            }, modifier = Modifier.weight(1f))
            RoundButton(text = "/", onClick = {
                currentValue = CalculatorNativeLib.division()
            }, modifier = Modifier.weight(1f))
            RoundButton(text = "8", onClick = {
                currentValue = CalculatorNativeLib.eight()
            }, modifier = Modifier.weight(1f))
            RoundButton(text = "5", onClick = {
                currentValue = CalculatorNativeLib.five()
            }, modifier = Modifier.weight(1f))
            RoundButton(text = "2", onClick = {
                currentValue = CalculatorNativeLib.two()
            }, modifier = Modifier.weight(1f))
            RoundButton(text = "0", onClick = {
                currentValue = CalculatorNativeLib.zero()
            }, modifier = Modifier.weight(1f))
        }
        Column(modifier = Modifier
            .fillMaxHeight()
            .weight(1f)
            .padding(10.dp),
            horizontalAlignment = Alignment.CenterHorizontally) {
            RoundButton(text = "M-", onClick = {
                CalculatorNativeLib.memoryMinus()
            }, modifier = Modifier.weight(1f))
            RoundButton(text = "X", onClick = {
                currentValue = CalculatorNativeLib.multiplication()
            }, modifier = Modifier.weight(1f))
            RoundButton(text = "9", onClick = {
                currentValue = CalculatorNativeLib.nine()
            }, modifier = Modifier.weight(1f))
            RoundButton(text = "6", onClick = {
                currentValue = CalculatorNativeLib.six()
            }, modifier = Modifier.weight(1f))
            RoundButton(text = "3", onClick = {
                currentValue = CalculatorNativeLib.three()
            }, modifier = Modifier.weight(1f))
            RoundButton(text = ".", onClick = {
                currentValue = CalculatorNativeLib.decimalPoint()
            }, modifier = Modifier.weight(1f))
        }
        Column(modifier = Modifier
            .fillMaxHeight()
            .weight(1f)
            .padding(10.dp),
            horizontalAlignment = Alignment.CenterHorizontally) {
            var columnHeight by remember{ mutableIntStateOf(0) }
            var buttonHeight by remember{ mutableIntStateOf(0) }
            RoundButton(text = "MR", onClick = {
                currentValue = CalculatorNativeLib.memoryRecall()
            }, modifier = Modifier.weight(1f))
            RoundButton(text = "⌫", onClick = {
                currentValue = CalculatorNativeLib.delete()
            }, modifier = Modifier.weight(1f))
            RoundButton(text = "-", onClick = {
                currentValue = CalculatorNativeLib.subtract()
            }, modifier = Modifier.weight(1f))

            Column(modifier = Modifier.weight(1f).onGloballyPositioned {
                columnHeight = it.size.height
            }, horizontalAlignment = Alignment.CenterHorizontally) {
                Button(onClick = {
                    currentValue = CalculatorNativeLib.add()
                }, modifier = Modifier
                    .size(80.dp)
                    .shadow(elevation = 3.dp, shape = CircleShape, clip = true)
                    .onGloballyPositioned {
                        buttonHeight = it.size.height
                    },
                    colors = ButtonDefaults.buttonColors(
                        containerColor = Color.White,
                        contentColor = colorResource(id = R.color.font_gray)
                    ),
                    shape = CircleShape
                ) {
                    Text(text = "+", fontSize = 18.sp, color = colorResource(R.color.font_gray))
                }
            }
            val height = getOVButtonHeight(LocalContext.current, columnHeight, buttonHeight)
            OvalVerticalButton(text = "=", onClick = {
                currentValue = CalculatorNativeLib.equals()
            }, cModifier = Modifier.weight(2f), bModifier = Modifier.height(height))
        }
    }
}

fun getOVButtonHeight(context: Context, columnHeight: Int, buttonHeight: Int): Dp {
    val height = columnHeight + buttonHeight
    return context.pxToDp(height.toFloat())
}

fun Context.pxToDp(px: Float): Dp {
    return with(Density(this)) { px.toDp() }
}